package finance

import grails.test.*
import org.apache.commons.math.random.RandomDataImpl

class LoMacKinlayServiceTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }
    
    protected void tearDown() {
        super.tearDown()
    }
    
    def loMacKinlayService
    
    void testEvenItems() {
        def items = 0..4
        def outcome = loMacKinlayService.evenItems(items)
        
        assertEquals "Incorrect size", 3, outcome.size()
        assertEquals outcome[0], 0	
        assertEquals outcome[1], 2	
        assertEquals outcome[2], 4
    }
    
    void testVariance() {
        def input = [
            Math.exp(1),
            Math.exp(2),
            Math.exp(3)
        ]
        def mu = 1 
        def sigma2 = Math.pow( 2 - 1 - mu, 2)
        sigma2 += Math.pow( 3 - 2 - mu, 2)
        sigma2 /= 2
        
        def outcome = loMacKinlayService.variance(input)
        assertEquals sigma2, outcome, 0.001
    }
    
    void testRatio() {
        def uniform = []
        def rdi = new RandomDataImpl()
        
        for( i in 0..1000 * 1000 ) 
            uniform << rdi.nextUniform(1, 100)
        
        def uniformRatio = loMacKinlayService.ratio( uniform )
        assertEquals 0, uniformRatio, 0.01
    }
}
